package edu.gatech.cs2340.tigershroud;

//necessary imports
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/** DateFilterActivity allows the user to select a date. 
 * DateFilterActivity then populates a list of the returned results
 * @author [InsertNameHere]
 * @version 9.0x, Build April 3, 2012.
 */

public class DateFilterActivity extends ListActivity implements RadioGroup.OnCheckedChangeListener
{
	//Instance variables
	private TodoDataSource ds; 
	private String key, order, num, utype;
	private TextView label, count;
	private RadioGroup dfilter;
	private int rselect;
	private List<Task> tlist;
	private ListView lv;
	private Button bback, bfilter;
	
	/**
	 * The onCreate method is a default android method that is called when the
	 * activity is first started. This onCreate method sets up the listeners for RadioButtons, ListViews, and Buttons; 
	 * determines the layout of the activity; and sets listeners for items within the activity
	 * @param saveInstanceState the Bundle name value pair that represents the state of the object
	 *
	 */
    @Override
	public void onCreate(Bundle savedInstanceState) 
    {
    	//Default calll to super class
        super.onCreate(savedInstanceState);
        
        //set the layout using the datefilter.xml file
        setContentView(R.layout.datefilter);
        
        //Set ds to refer to the database 
        ds = new TodoDataSource(this);
        
        //open the database
	    ds.open();
	    
	    //Get any/all object(s) that were passed to the activity
	    key = getIntent().getExtras().getString("key");
	    order = getIntent().getExtras().getString("order");
        utype = getIntent().getExtras().getString("utype");
      
	    //Set the layout for all of the items in the activity
	    label = (TextView) findViewById(R.id.tvdates);	
	    count = (TextView) findViewById(R.id.tvcount);	
	    dfilter = (RadioGroup) findViewById(R.id.rgdate);
	    bback = (Button) findViewById(R.id.bback);
	    bfilter = (Button) findViewById(R.id.bfilter);
	    label.setText(key);
	    
	    //set a checked listener for the activity
	    dfilter.setOnCheckedChangeListener(this);
	    
	    //populate the list with a query from the database
	    tlist = ds.searchByDate(key, order, utype);
	    
	    //refresh the list so it displays the most current information
	    refresh(tlist);
	    
	    //generate the ListView
       	lv = getListView();
       	
       	//set attributes to ListView
        lv.setTextFilterEnabled(true);
        
        //Uses an anonymous inner-class to add the listener to the ListView
        lv.setOnItemClickListener(new OnItemClickListener() 
        {	
        	/**
        	 * This method inside the anonymous inner-class OnItemClickListener
        	 * determines the behavior of the application when the item is clicked
        	 * @param parent the AdapterView the outer Adapter that allows viewing the contents
        	 * @param view the View that is generated by the parent activity
        	 * @param long id in R.java that represents the item
        	 */
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        	{
        		Task t = (Task) lv.getItemAtPosition(position);
        		Toast.makeText(DateFilterActivity.this, t.getTaskName() + " selected...", Toast.LENGTH_LONG).show();
        		Log.d("QQQQQQ", "key: " + key + "  clicked: " + t.getDnum() + "   -> " + (Long.parseLong(key)-t.getDnum()));
        		Intent i = new Intent(DateFilterActivity.this, TaskViewActivity.class);
        		i.putExtra("clickedTask", t);
        		startActivity(i);
            }
        });
        
        //determine the behavior for the back button
	    bback.setOnClickListener(new View.OnClickListener() 
	    {
	    	/**
	    	 * This method inside the anonymous inner class OnClickListener determines behavior when clicked
	    	 * @param v the the View of the activity that holds the item
	    	 */
	    	public void onClick(View v) 
			{
	    		//close the database and finish the activity
				ds.close();
				finish();
			}
		});
	    
	    //determine the behavior for the filter button
	    bfilter.setOnClickListener(new View.OnClickListener() 
	    {
	    	/**
	    	 * This method inside the anonymous inner class OnClickListener determines behavior when clicked
	    	 * @param v the the View of the activity that holds the item
	    	 */
	    	public void onClick(View v) 
			{
	    		refresh(tlist);
			}
		});
    }

    //!!!!Hey this takes in an int for the checkedId, but it actually doesn't use that. Can we not include that param
    /**
	 * This method determines the behavior of the activity when the user changes
	 * the selection from the default selection. The activity passes in the name of
	 * the RadioGroup and the value of the checked item. Depending on which button is clicked,
	 * the ListView is populated with different items
	 * @param rg the the RadioGroup that item belongs to
	 * @param checkedId the value of the selected item within the RadioButtonGroup
	 */
	public void onCheckedChanged(RadioGroup rg, int checkedId) 
	{
		// determine which RadioButton was checked
		rselect = rg.getCheckedRadioButtonId();
		
		//on switch: the id of the RadioButton inside the RadioButtonGroup
		switch(rselect)
		{
			case R.id.rbbefore:
				tlist = ds.beforeDate(key, order, utype);
				Log.d("D", "clicked before");
				break;
			case R.id.rbafter:
				tlist = ds.afterDate(key, order, utype);
				Log.d("D", "clicked after");
				break;
			case R.id.rbon:
				tlist = ds.searchByDate(key, order, utype);
				Log.d("D", "clicked on");
				break;
		}
	}
 /**
	 * This method refreshes the the ListView widget's List. After the List is refreshed,
	 * any/all changes are reflected in the user interface.
	 * @param t the list of tasks that is passed to the ArrayAdapter
	 */
    public void refresh(List<Task> t)
    {
    	//Set up the ListAdapter
        setListAdapter(new ArrayAdapter<Task>(getApplicationContext(), android.R.layout.simple_list_item_1, t));
        //If the list has no items, display no items
        if(t.size() == 1)
        {
        	if((t.get(0)).getDnum() == -1)
        	{
        		num = "0";
        	}
        }
        //else the list must be populated, continue
        else
        {
        	num = t.size() + "";
        }
        //display to the user the number of items returned
        count.setText(num);
    }
}
